<?php

	class fluxJavascript {

		protected function toJs($FCoM) {

			if ($FCoM['type'] == 'function')
				$result = call_user_func ($FCoM['call'],$this->superGlobal);

			if ($FCoM['type'] == 'class')
				$result = new $$FCoM['call'] ($this->superGlobal);

			if ($FCoM['type'] == 'method') {

					  if (!fluxObject::isMethodOf ($FCoM['class'],$FCoM['call'])) {

							$msg = 'Method ' . $FCoM['call'] .' doesnt exists in ' . $FCoM['class'];

							throw (new fluxXception ($msg));

					  }

					  $class = new $FCoM['class']();
					  $result = $class->$FCoM['call']();
			}

			return $this->parseResult($result).';';

		}

		private function parseResult ($result) {

				$code = NULL;

			if(is_array($result))
				$code = $this->arrayToJsObject($result);

			if (is_object ($result))
				$code = $this->objectToJsObject();

			if (is_bool ($result))
				$code = $this->boolToJsObject();

			return $code;

		}

		private function arrayToJsObject($result){

			$c = 0;

			$jsObj = NULL;
			

			if (isset ($result['lajaMessage']))
				$jsObj .= 'construct:this.Message = "' . $result['lajaMessage'] . '";' . "\n";
			else
				$jsObj .= 'construct:this.Message = "LAJA: No message";' . "\n";

			if (isset ($result['lajaRetValue']))
				$jsObj .= 'construct:this.lajaRetValue = "' . $result['lajaRetValue'] . '";' . "\n";
			else
				$jsObj .= 'construct:this.lajaRetValue = "void";' . "\n";


			$jsObj .= 'construct:this.object=';

			$code   = self::jsArray ($result,TRUE,array('lajaMessage','lajaRetValue'),NULL);
			$code   = ($code) ? '['.$code.']' : 'null';
			$code  .=";\n";
			$jsObj .= $code;

			if ($code == 'null') {
					  $jsObj='null';
					  return $jsObj;
			}

			$jsObj .= 'getMessage:  return (this.Message);'. "\n";
			$jsObj .= 'getReturnValue:var ret = new Number (this.lajaRetValue);' . "\n";
			$jsObj .= 'getReturnValue:return ret.valueOf();'. "\n";

			
			$jsObj .= 'test:alert ("test");' . "\n";	

			return $jsObj;

		}

		public static function jsArray ($array,$call=FALSE) {

			$finalCode=NULL;
			$x = 1;

			if (!sizeof($array)||!is_array($array))
					  return FALSE;

			for ($i=0;isset($array[$i]);$i++) {

				$finalCode .= '[{';

				$s = sizeof($array[$i]);

				while (list ($key,$value) = each ($array[$i])){

					if (is_int($key))
						  continue;

					if ($x<$s)
						  $finalCode .=$key.':'."'".$value."',";
					else
						  $finalCode .=$key.':'."'".$value."'";
					$x++;

				}

				$x=1;

				if (isset ($array[$i+1]))
					  $finalCode .= '}],';
				else
					  $finalCode .= '}]';
			
			}

			return $finalCode;

		}

/*

		public static function jsArray ($array,$double=TRUE,$avoid=NULL,$key=NULL) {

			if (!is_array ($array))
					  throw (new fluxXception ('jsArray expects an array to be parse'. gettype($array) . 'given'));

			$arr  = NULL;
			$flag = NULL;

			if ($double)
				$arr.='[';

			foreach ($array as $k => $v) {

				if (is_array ($avoid)) {
					$keys = array_values ($avoid);
					if (in_array ($k,$keys))
						continue;

				}

				$arr.='[';

				if (is_array ($v)) {

					$arr.=self::jsArray ($v,FALSE,$avoid,$k);
					continue;

				}

				if (!is_int ($k))
						  $k = "'" . $k . "'";

				if (!is_int ($v))
						  $v = "'" . $v . "'";

				$arr .= $k.','.$v;

				$arr .= '],';
			}

			$arr = substr ($arr,0,-1);

			$arr .= (!$double) ? ']' : '];';

			return ($arr);

		}
*/

		public static function openTag  () {
				  return '<script type="text/javascript">' . "\n";
		}

		public static function closeTag  () {
				  return '</script>' . "\n";
		}

		public static function loadScripts ($scriptsDir=NULL,$echo=FALSE) {

			$dirContents  = fluxFile::directoryRead ($scriptsDir,TRUE);

			$jsCode = NULL;

			$jsCode = self::openTag();

			$script  = NULL;
			$scripts = NULL;

			foreach ($dirContents as $scriptFile) {

				if (!preg_match ('#\.js$#',$scriptFile))
					continue;

				$script .= fluxFile::read ($scriptFile);
				$scripts.=$scriptFile . ', ';

			}

			if (empty ($script))
					  throw (new fluxXception ('Got no script contents! given scripts ->' . $scripts));

			unset ($scripts);

			$jsCode .= $script;

			$jsCode .= "\n" . self::closeTag();

			if ($echo){
				echo $jsCode;
				return;
			}

			return $jsCode;

		}


	} //EOC

?>
